#include "insetrument_set.h"

Insetrument_Set::Insetrument_Set(QWidget *parent) : QMainWindow(parent)
{
    Sysctrl->SYS_CTL_MODE =0;
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    timer = new QTimer(this);
    timer->start(1000);

    UI_Init();
    connect_Init();
    update_data();
}
void Insetrument_Set::UI_Init()
{

    the_btn_style[0]=("\
                      outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                      background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                      border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                      border-style: outset;\
                      /* 按钮背景图标设置 */\
                      background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                      background-origin: content;\
                      background-position: left;	         /* 背景图片的位置 */\
                      padding-right: 0px;	    /*内边距设置*/	    \
                      padding-left: 10px;		/*  */	\
                      padding-bottom: 10px;		/*  */    \
                      padding-top: 10px;		/*  */    \
                      background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                      /* 按钮文本设置 */\
                      text-align: content;			             /* 文本的位置 */\
                      font-size: 18px;\
                      color: #FFFFFF;			                 /* 文本颜色 */\
                 "
);

//
the_btn_style[2]=("QPushButton:pressed{background-color:rgb(99,211,133);}");
the_btn_style[1]=("\
                  outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                  border-radius: 8px;		                 /* 按钮边框的圆角设置 */\
                  border-style: outset;\
                  /* 按钮背景图标设置 */\
                  background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */\
                  background-origin: content;\
                  background-position: left;	         /* 背景图片的位置 */\
                  padding-right: 0px;	    /*内边距设置*/	    \
                  padding-left: 10px;		/*  */	\
                  padding-bottom: 10px;	/*  */   \
                  padding-top:10px;		/*  */    \
                  background-repeat: repeat;	         /* 设置背景图像的平铺模式 */\
                  /* 按钮文本设置 */\
                  text-align:content ;			             /* 文本的位置 */\
                  font-size: 22px;\
                  color: rgb(225, 225, 225);			                 /* 文本颜色 */\
                  ");

                  for(int i=0;i<6;i++)//创建主屏幕显示的按键
                  {
                       theBtn[i] = new QPushButton(this);
              //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
                      theBtn[i]->setFixedSize(150,40);
                      theBtn[i]->geometry();
                      btnlist_ui.append(theBtn[i]);
                  }
                  theBtn[0]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}");
                  theBtn[1]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}");
                  theBtn[2]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}");
                  theBtn[3]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}");
                  theBtn[4]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}");
                  theBtn[5]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}");

                 btnlist_ui.at(0)->setText(tr("     I/O扩展板"));
                 btnlist_ui.at(1)->setText(tr("     气阀"));
                 btnlist_ui.at(2)->setText(tr("     臭氧发生器"));
                 btnlist_ui.at(3)->setText(tr("     光度计"));
                 btnlist_ui.at(4)->setText(tr("     渗透炉"));
                 btnlist_ui.at(5)->setText(tr("     返回"));

                 for(int i=0;i<5;i++)//创建主屏幕显示的按键
                 {
                      theradioBtn[i] = new radioswitch(this);
                     theradioBtn[i]->setFixedSize(150,40);
                     btnlist_ui2.append(theradioBtn[i]);
                 }
                 btnlist_ui2.at(1)->setTextOn("6");
                 btnlist_ui2.at(1)->setTextOff("3");

                  for(int i=0;i<3;i++)//创建主屏幕显示的按键
                  {
                       theLabel[i] = new QLabel(this);
                  }
                  theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
                  theLabel[0]->setStyleSheet("QLabel{"
                                        + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
                  theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
                  theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

                 theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
                 theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
                 theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
                 theLabel[0]->setText("串口设置");


                 //事件过滤器
                 foreach (QPushButton *btn, btnlist_ui) {
                     btn->installEventFilter(this);
                 }

                 QGridLayout *theLayout = new QGridLayout();
//                 theLayout->setAlignment(Qt::AlignCenter);
                 theLayout->addWidget(btnlist_ui.at(0), 1, 0);
                 theLayout->addWidget(btnlist_ui.at(1), 2, 0);
                 theLayout->addWidget(btnlist_ui.at(2), 3, 0);
                 theLayout->addWidget(btnlist_ui.at(3), 4, 0);
                 theLayout->addWidget(btnlist_ui.at(4), 5, 0);
                 theLayout->addWidget(btnlist_ui.at(5), 6, 0);
                 theLayout->addWidget(btnlist_ui2.at(0), 1, 1);
                 theLayout->addWidget(btnlist_ui2.at(1), 2, 1);
                 theLayout->addWidget(btnlist_ui2.at(2), 3, 1);
                 theLayout->addWidget(btnlist_ui2.at(3), 4, 1);
                 theLayout->addWidget(btnlist_ui2.at(4), 5, 1);

                 theLayout->setContentsMargins(50,90,50,50);//(int left, int top, int right, int bottom)
                 theLayout->setSizeConstraint(QLayout::SetMinimumSize);
                   setLayout(theLayout);

                   QWidget* widget = new QWidget(this);
                   widget->setGeometry(0,0,640,330);
                   widget->setLayout(theLayout);
                   }
void Insetrument_Set::update_data()
                   {


    if(O3_Show_Flag)
    {
    btnlist_ui2.at(2)->setChecked(1);
    btnlist_ui2.at(2)->m_startX=110;  //on
    }
    else
    {
    btnlist_ui2.at(2)->setChecked(0);
    btnlist_ui2.at(2)->m_startX=0;//off
    }
    if(Photometer_Flag)
    {
    btnlist_ui2.at(3)->setChecked(1);
    btnlist_ui2.at(3)->m_startX=110;  //on
    }
    else
    {
    btnlist_ui2.at(3)->setChecked(0);
    btnlist_ui2.at(3)->m_startX=0;//off
    }

    if(Sysset->Gas_Num == 6)
    {
        btnlist_ui2.at(1)->setChecked(1);
        btnlist_ui2.at(1)->m_startX=110;
    }
    else {
        btnlist_ui2.at(1)->setChecked(0);
        btnlist_ui2.at(1)->m_startX=0;
    }

}

void Insetrument_Set::connect_Init()
{

    //定时器
    connect(timer,&QTimer::timeout,[=](){
//update_data();
    });

//    //IO扩展板
//    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
//       if(!btnlist_ui2.at(0)->getChecked())
//       {
//           btnlist_ui2.at(0)->setChecked(1);
//           btnlist_ui2.at(0)->m_startX=110;
//       }
//       else {
//           btnlist_ui2.at(0)->setChecked(0);
//           btnlist_ui2.at(0)->m_startX=0;
//       }


//    });

    //气阀
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        if(!btnlist_ui2.at(1)->getChecked())
        {
            btnlist_ui2.at(1)->setChecked(1);
            btnlist_ui2.at(1)->m_startX=110;
            Sysset->Gas_Num = 6;
        }
        else {
            btnlist_ui2.at(1)->setChecked(0);
            btnlist_ui2.at(1)->m_startX=0;
            Sysset->Gas_Num = 3;
        }
    });

    //臭氧发生器
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){

        if(flag_protect)
        {
            if(O3_Show_Flag)
            {
                O3_Show_Flag=0;
                Sysset->Sys_Ozo_Sel=0;
                Sysctrl->Sys_Ozo_Sel=Sysset->Sys_Only_Ozo = 0;
                Sysset->IS_OZO_ENABLE = 0;
                Sysctrl->BD_Ozonator.Vot_Set = 0;
                O3_RN[0]=O3_RN[1]=O3_RN[2]="";
                btnlist_ui2.at(2)->setChecked(0);
                btnlist_ui2.at(2)->m_startX=0;//off
            }
            else
            { //on
                btnlist_ui2.at(2)->setChecked(1);
                btnlist_ui2.at(2)->m_startX=110;
                O3_Show_Flag=1;
            }
        }

    });

    //光度计
    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){


        if(flag_protect)
        {
            if(Photometer_Flag)
            {
                Photometer_Flag=0;
                Sysctrl->PhotoMeter_Mode=Sysset->Sys_Run[1]=0;
                Sysctrl->BD_Interface.Fflag = 1;//阀门控制切换标志 0:出来的时候为0  1:进去的时候为1
                Sysctrl->BD_Photometer.Vot_Set =0;
                Sysset->PHOT_ENABLE = 0;
                Photometer_RN[0]=Photometer_RN[1]=Photometer_RN[2]="";
                if(Sysctrl->PhotoMeter_Mode == 0)
                {
                    flag_daojin = 1;
                    photo_flag=0;
                    daoji=300;
                    Sysset->Phot_Cfg=Photometer_Flag;
                    //保存
                    Sysctrl->Para_Save = 1;
                    btnlist_ui2.at(3)->setChecked(0);
                    btnlist_ui2.at(3)->m_startX=0;
                }
            }
            else
            {
                Photometer_Flag=1;
                Sysset->Phot_Cfg=Photometer_Flag;
                //保存
                Sysctrl->Para_Save = 1;
                btnlist_ui2.at(3)->setChecked(1);
                btnlist_ui2.at(3)->m_startX=110;
            }
        }

    });

//    //渗透炉
//    connect(btnlist_ui.at(4),&QPushButton::clicked,[=](){
//        if(!btnlist_ui2.at(4)->getChecked())
//        {
//            btnlist_ui2.at(4)->setChecked(1);
//            btnlist_ui2.at(4)->m_startX=110;
//        }
//        else {
//            btnlist_ui2.at(4)->setChecked(0);
//            btnlist_ui2.at(4)->m_startX=0;
//        }
//    });


    //返回按钮
    connect(btnlist_ui.at(5),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
        Sysctrl->Para_Save = 1;
    });


    connect(btnlist_ui2.at(1),&radioswitch::statusChanged,[=](){
        if(!btnlist_ui2.at(1)->getChecked())
        {
            btnlist_ui2.at(1)->setChecked(1);
            btnlist_ui2.at(1)->m_startX=110;
            Sysset->Gas_Num = 6;
        }
        else {
            btnlist_ui2.at(1)->setChecked(0);
            btnlist_ui2.at(1)->m_startX=0;
            Sysset->Gas_Num = 3;
        }

    });

    connect(btnlist_ui2.at(2),&radioswitch::statusChanged,[=](){
        if(flag_protect)
        {
            if(O3_Show_Flag)
            {
                O3_Show_Flag=0;
                Sysset->Sys_Ozo_Sel=0;
                Sysctrl->Sys_Ozo_Sel=Sysset->Sys_Only_Ozo = 0;
                Sysset->IS_OZO_ENABLE = 0;
                Sysctrl->BD_Ozonator.Vot_Set = 0;
                O3_RN[0]=O3_RN[1]=O3_RN[2]="";
                btnlist_ui2.at(2)->setChecked(0);
                btnlist_ui2.at(2)->m_startX=0;//off
            }
            else
            { //on
                btnlist_ui2.at(2)->setChecked(1);
                btnlist_ui2.at(2)->m_startX=110;
                O3_Show_Flag=1;
            }
        }
        else{
            btnlist_ui2.at(3)->setEnabled(0);
                }
    });

    connect(btnlist_ui2.at(3),&radioswitch::statusChanged,[=](){
        if(flag_protect)
        {
            if(Photometer_Flag)
            {
                Photometer_Flag=0;
                Sysctrl->PhotoMeter_Mode=Sysset->Sys_Run[1]=0;
                Sysctrl->BD_Interface.Fflag = 1;//阀门控制切换标志 0:出来的时候为0  1:进去的时候为1
                Sysctrl->BD_Photometer.Vot_Set =0;
                Sysset->PHOT_ENABLE = 0;
                Photometer_RN[0]=Photometer_RN[1]=Photometer_RN[2]="";
                if(Sysctrl->PhotoMeter_Mode == 0)
                {
                    flag_daojin = 1;
                    photo_flag=0;
                    daoji=300;
                    Sysset->Phot_Cfg=Photometer_Flag;
                    //保存
                    Sysctrl->Para_Save = 1;
//                    btnlist_ui2.at(3)->setChecked(0);
//                    btnlist_ui2.at(3)->m_startX=0;
                }
            }
            else
            {
                Photometer_Flag=1;
                Sysset->Phot_Cfg=Photometer_Flag;
                //保存
                Sysctrl->Para_Save = 1;
//                btnlist_ui2.at(3)->setChecked(1);
//                btnlist_ui2.at(3)->m_startX=110;
            }
        }
        else{
            btnlist_ui2.at(3)->setEnabled(0);
                }
    });

}

bool Insetrument_Set::eventFilter(QObject *watched, QEvent *event)
    {

            for(int i=0;i<btnlist_ui.length();i++)
                if(btnlist_ui.at(i) == watched)
            {
                if(event->type() == QEvent::KeyPress)
                {
                   QKeyEvent *ev = (QKeyEvent *)event;
                   QPushButton *button = (QPushButton *)watched;
                   switch (ev->key())
                   {
                   case OK:
                     {
                       emit  button->clicked();
                       break;
                     }
                   case UP://上
                   {
                       if(focusWidget() != btnlist_ui.first())
                       focusPreviousChild();
                       break;
                   }
                   case DOWN://下
                   {
                       if(focusWidget() != btnlist_ui.back())
                       focusNextChild();
                       break;
                   }
                   case LEFT://左
                   {
                     if(focusWidget() != btnlist_ui.first())
                          focusPreviousChild();
                     break;
                   }
                   case RIGHT://右
                   {
                     if(focusWidget() != btnlist_ui.back())
                          focusNextChild();
                  break;
                   }
                   case INIT: //打开初始化界面
                   {
                       init_Show *show = new init_Show(this);
                       show->show();
                       this->hide();
                       break;
                   }
                   case BACK:
                   {
                       this->parentWidget()->show();
                       this->close();
                       break;
                   }
                   default:
                       break;
                   }
                }

                }
           return QMainWindow::eventFilter(watched,event);//返回父类的事件过滤器

    }
